'use strict';
const dayjs = require('dayjs');

module.exports = app => {
    const { STRING, INTEGER } = app.Sequelize;
    const consult =  app.model.define(
        'consult',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            member_id: INTEGER,
            order_id: INTEGER,
            status: INTEGER,
            content: STRING,
            create_time: {
                type: app.Sequelize.DATE,
                allowNull: false,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                },
            }
        },
        {
            tableName: 'consult',
            timestamps: true, // 启用时间戳(createdAt, updatedAt)
            updatedAt: 'updated_time', // 不想要 updatedAt
            createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
        }
    );
    // diagnosis 用户得诊断记录
    consult.associate = function() {
        app.model.Consult.belongsTo(app.model.Member, {foreignKey: 'member_id', targetKey: 'id'});
        // 一个咨询对应多个评论
        app.model.Consult.hasMany(app.model.Comment, {foreignKey: 'consult_id', targetKey: 'id', as: 'commentList'});
        // 订单
        app.model.Consult.belongsTo(app.model.OrderGoods, {foreignKey: 'order_id', targetKey: 'id', as: 'order'});
    }
    return consult
};
